/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pro.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.persistence.ResponseResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pro.entity.BasBuilding;
import com.thinkgem.jeesite.modules.pro.service.BasBuildingService;

/**
 * 楼栋管理Controller
 * @author dby
 * @version 2020-10-15
 */
@Controller
@RequestMapping(value = "${adminPath}/pro/basBuilding")
public class BasBuildingController extends BaseController {

	@Autowired
	private BasBuildingService basBuildingService;
	
	@ModelAttribute
	public BasBuilding get(@RequestParam(required=false) String id) {
		BasBuilding entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = basBuildingService.get(id);
		}
		if (entity == null){
			entity = new BasBuilding();
		}
		return entity;
	}
	
	@RequiresPermissions("pro:basBuilding:view")
	@RequestMapping(value = {"list", ""})
	public String list(BasBuilding basBuilding, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BasBuilding> page = basBuildingService.findPage(new Page<BasBuilding>(request, response), basBuilding); 
		model.addAttribute("page", page);
		return "modules/pro/basBuildingList";
	}

	@RequiresPermissions("pro:basBuilding:view")
	@RequestMapping(value = "form")
	public String form(BasBuilding basBuilding, Model model) {
		model.addAttribute("basBuilding", basBuilding);
		return "modules/pro/basBuildingForm";
	}

	@RequiresPermissions("pro:basBuilding:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public ResponseResult save(BasBuilding basBuilding, Model model, RedirectAttributes redirectAttributes) {

		if(null==basBuilding || StringUtils.isBlank(basBuilding.getBuildingNumber())){
			return ResponseResult.failure("楼栋编号不能为空！");
		}
		int count = basBuildingService.findBuildingNumber(basBuilding);
		if(count>0){
			return ResponseResult.failure("楼栋编号已存在！");
		}

		basBuildingService.save(basBuilding);
		return ResponseResult.success();
	}
	
	@RequiresPermissions("pro:basBuilding:edit")
	@RequestMapping(value = "delete")
	public String delete(BasBuilding basBuilding, RedirectAttributes redirectAttributes) {
		basBuildingService.delete(basBuilding);
		addMessage(redirectAttributes, "删除楼栋管理成功");
		return "redirect:"+Global.getAdminPath()+"/pro/basBuilding/?repage";
	}

	@RequiresPermissions("pro:basBuilding:view")
	@RequestMapping(value = "view")
	public String view(BasBuilding basBuilding, Model model) {
		model.addAttribute("basBuilding", basBuilding);
		return "modules/pro/basBuildingView";
	}


}